﻿using System;
using System.Linq;
using Enyim.Caching;
using Ayatta.Contract;
using System.Web.Mvc;
using Web.Core.Security;
using Web.Pay.Models;
using Ayatta.Contract.Domain;
using System.Collections.Generic;


namespace Web.Pay.Controllers
{
    public class RechargeController : BaseController
    {
        private const string ResultUrl = "/recharge/result/";

        public RechargeController(Lazy<IInfrastructureService> infrastructure, Lazy<IPassportService> passport, Lazy<IAccountService> account, Lazy<IStoreService> store, Lazy<ITradeService> trade, Lazy<IPlayService> play, Lazy<IMiscService> misc, Lazy<IExtraService> extra, IMemcachedClient memcachedClient)
            : base(infrastructure, passport, account, store, trade, play, misc, extra, memcachedClient)
        {
        }

        /// <summary>
        /// 账户充值
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {

            var identity = User.Identity.Identity();
            var model = new RechargeControllerModel.Index();

            var platforms = PaymentPlatformList();
            var tempEBanks = PaymentEBankList();
            var packages = RechargePackageList(identity.Id);
            var eBanks = new List<PaymentEBank>();

            foreach (var platform in platforms)
            {
                var p = platform;
                var x = eBanks.Select(o => o.BankId);
                var temp = tempEBanks.Where(o => o.PlatformId == p.Id && o.Status && !x.Contains(o.BankId));
                eBanks.AddRange(temp);
            }

            model.Packages = packages;
            model.Platforms = platforms;
            model.EBanks = eBanks;
            return View(model);
        }

        /// <summary>
        /// 拍币充值
        /// </summary>
        /// <returns></returns>
        public ActionResult Coin()
        {
            return View();
        }

        /// <summary>
        /// 游戏充值
        /// </summary>
        /// <returns></returns>
        public ActionResult Game()
        {
            return View();
        }
       

        public ActionResult ByEBank(RechargeCategory param,int packageId,int eBankId,int amount)
        {
            var identity = User.Identity.Identity();
            var model = new MagicKit<Magic<int, RechargeCategory, RechargePackage, PaymentEBank>>();
          
            var magic = new Magic<int, RechargeCategory, RechargePackage, PaymentEBank>(amount,param);
            var package = RechargePackageList(identity.Id).FirstOrDefault(o => o.Id == packageId && o.Category==param);
            if(package!=null)
            {
                magic.Third = package;
                var eBank = PaymentEBankList().FirstOrDefault(o => o.Id == eBankId);
                if(eBank!=null)
                {
                    magic.Fourth = eBank;
                }
                else
                {
                    model.Message = "银行参数错误！";
                }
            }
            else
            {
                model.Message = "充值包参数错误！";
            }
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult ByEBank(int packageId, int eBankId, int amount)
        {
            var identity = User.Identity.Identity();
           
            var package = RechargePackageList(identity.Id).FirstOrDefault(o => o.Id == packageId);
            if (package != null)
            {
                var total = amount;
                var eBank = PaymentEBankList().FirstOrDefault(o => o.Id == eBankId);
                if (eBank != null)
                {
                    var pay = GetOnlinePay(eBank.PlatformId);
                    if (pay != null)
                    {
                        var dateTime = DateTime.Now;
                        var recharge = new AccountRecharge();
                        var payment = new AccountPayment();
                        
                        recharge.Id = AccountRecharge.NewId();

                        recharge.UserId = identity.Id;
                        recharge.Category = package.Category;
                        recharge.PackageId = package.Id;
                        recharge.Amount = amount;
                        recharge.Discount = 0;
                        recharge.Total = total;
                        recharge.Gateway = eBank.PaymentBank.Name;
                        recharge.ForUser = false;
                        recharge.ForUserId = 0;
                        recharge.ForUserName = string.Empty;
                        recharge.GameId = 0;
                        recharge.ServerId = 0;
                        recharge.ForeignKey = string.Empty;
                        recharge.Extra = string.Empty;
                        recharge.Status = false;
                        recharge.SubStatus = false;
                        recharge.CreatedBy = User.Identity.Name;
                        recharge.CreatedOn = dateTime;

                        var id = AccountPayment.NewId();
                        var payNo = id;
                        if (eBank.PlatformId == Platform.ChinaPay)
                        {
                            payNo = AccountPayment.NewPayNo();
                        }
                        payment.Id = id;
                        payment.PayNo = payNo;
                        payment.UserId= identity.Id;
                        payment.Category = PaymentCategory.Recharge;
                        payment.SubCategory =(byte) recharge.Category;
                        payment.ForeignKey = recharge.Id;
                        payment.Amount = amount;
                        payment.Balance = 0;
                        payment.Method =PaymentMethod.EBank;
                        payment.EBankId = eBank.Id;
                        payment.PlatformId = eBank.PlatformId;
                        payment.Gateway = eBank.GatewayCode;
                        payment.BankCard = BankCard.Debit;
                        payment.BankName = eBank.PaymentBank.Name;
                        payment.CardNo= string.Empty;
                        payment.CardPwd= string.Empty;
                        payment.CardAmount = 0;
                        payment.IpAddress = GetIpAddress();
                        payment.ExternalNo= string.Empty;
                        payment.Flag = 0;
                        payment.Extra = string.Empty;
                        payment.Status = false;
                        payment.CreatedBy = User.Identity.Name;
                        payment.CreatedOn = dateTime;

                        var status = Account.Value.AccountPaymentRechargeCreate(recharge, payment);
                        if (status)
                        {
                            //var url = pay.GetPayUrl(payment);
                           // return Redirect(url);
                            return Redirect(ResultUrl +5);//

                        }
                        return Redirect(ResultUrl + 4);//创建订单失败
                    }
                    return Redirect(ResultUrl + 3);//支付平台参数错误
                }
                return new RedirectResult(ResultUrl + 2);//银行参数错误
            }
            return new RedirectResult(ResultUrl + 1);//充值包参数错误
        }

        public ActionResult ByPhoneCard(string cardNo, string cardPwd, int cardAmount)
        {
            var identity = User.Identity.Identity();
            var model = new MagicKit<string>();

            return Json(model);
        }
    }
}
